var app = getApp();
var api = require('../../api.js');
var tabbarTemplate = require("../tab-bar/tab-bar.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    tabbarTemplate.tabbar("tabBar", 0, this, 'vip')
    this.getVipList()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var page = this
    var user = wx.getStorageSync('user_info');
    return {
      path: '/pages/index/index?pid=' + user.user_id,
    }
  },

  //vip列表
  getVipList:function(){
    var page = this;
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.user.vip_list,
      method: 'POST',
      data: {
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            vip_list: res.data.vip_list,
            user_vip_info: res.data.user_vip_info,
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  //点击购买vip
  buyvip:function(e){
    var page = this;
    var id = e.currentTarget.dataset.id
    if (page.data.user_vip_info != false && page.data.user_vip_info.id !=id){
      wx.showModal({
        title: '警告',
        content: '您当前的' + page.data.user_vip_info.title + '有效期到' + page.data.user_vip_info.due_time+'。\n开通新的VIP会使存在的VIP失效。\n 确认开通',
        success(res){
          if(res.confirm){
            page.submitByvip(id);
          }
        }
      })
    }else{
      page.submitByvip(id);
    }
  },
  //续费vip
  continueVip:function(){

  },
  //提交购买VIP订单
  submitByvip:function(vid){
    var page = this;
    wx.showLoading({
      title: '提交中',
    })
    app.request({
      url: api.order.buy_vip,
      method: 'POST',
      data: {
        id:vid
      },
      success: function (res) {
        if (res.errcode == 0) {
          if(res.data.core_id){
            app.request({
              url: api.order.get_pay_data,
              method: 'POST',
              data: {
                order_id: res.data.core_id,
                pay_type: 'WECHAT_PAY'
              },
              success: function (result) {
                wx.hideLoading();
                if (result.errcode == 0) {
                  var pay_data = result.data;
                  wx.requestPayment({
                    timeStamp: pay_data.timeStamp,
                    nonceStr: pay_data.nonceStr,
                    package: pay_data.package,
                    signType: pay_data.signType,
                    paySign: pay_data.paySign,
                    success: function (res) {
                      wx.showToast({
                        title: '订单支付成功',
                        icon: 'success'
                      });
                      // setTimeout(function () {
                      //   this.getVideoInfo();
                      //   // video.is_pay = 0;
                      //   // page.setData({
                      //   //   video: video
                      //   // });
                      //   // page.playVideo();
                      // }, 2000)
                    },
                    fail: function (res) {
                      wx.showToast({
                        title: '订单未支付',
                        image: '/images/icon-warning.png'
                      });
                    }
                  });
                } else {
                  wx.showModal({
                    title: '提示',
                    content: result.msg,
                    showCancel: false
                  });
                }
              }
            });
          }
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  //tabbar跳转
  tabBarRedirect: function (e) {
    wx.redirectTo({
      url: e.currentTarget.dataset.url,
    })
  }

})